package net;

import util.Matrix;

public class AssociativeNet extends SupervisedNet{
	private Number[][] w;
	protected int maxIterations;
	protected int totalIterations;


	public AssociativeNet(int inputNeurons, int outputNeurons) {
		super(inputNeurons, outputNeurons);
		w = new Number[inputNeurons][outputNeurons];
		maxIterations = 3;
	}

	@Override
	public Class classify(String file) {
		Number[] input = readFile(file);
		Class result = null;
		totalIterations = 1;
		do {			
			result = analize(input);
			if (result != null){
				return result;
			}
			input = Matrix.multiply(lastVerifiedOutput,Matrix.transpose(w))[0];
			applyActivationFunction(input);
			totalIterations++;			
		}while (totalIterations <= maxIterations);
		return null;
	}

	@Override
	public void learn() {
		w = new Number[inputLayer.size()][outputLayer.size()];
		Matrix.initialize(w);
		for(Sample conjunto:samples){
			w = Matrix.sum(w,Matrix.multiply(conjunto.getInput(),conjunto.getOutput()) );			
		}	
		setWeights(w);
	}

	public Number[][] getW() {
		return w;
	}

	public void setW(Number[][] w) {
		this.w = w;
	}

	public int getMaxIterations() {
		return maxIterations;
	}

	public void setMaxIterations(int maxIterations) {
		this.maxIterations = maxIterations;
	}

	public int getTotalIterations() {
		return totalIterations;
	}
	
	


}
